﻿//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
using System.Collections;
using System.Xml;
using System.Windows.Forms;
using System.Drawing;

namespace BtsToolPack
{

    public class AnalysisTool
    {

        public ArrayList al = new ArrayList();
        public ArrayList processErrors = new ArrayList();

        public BtsCatalogExplorer catalog = new BtsCatalogExplorer();


        private String _ConnectionString;

        public String ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value;

            catalog.ConnectionString = this.ConnectionString;
            }
        }


        private String _AppName;

        public String AppName
        {
            get { return _AppName; }
            set { _AppName = value; }
        }


        public AnalysisTool()
        {


        }


 
        public void GetAllAdresses(BtsNode tp)
        {
            GetAllReceivePortAdresses(tp);
            GetAllSendPortAdresses(tp);


        }

        public void GetAllReceivePortAdresses(BtsNode tp)
        {



            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING RECEIVE LOCATIONS");

                //Enumerate the receive locations in each of the receive ports.
                foreach (ReceivePort receivePort in catalog.ReceivePorts)
                {
                    //Console.WriteLine(receivePort.Name);
                    //Enumerate the receive locations.
                    foreach (ReceiveLocation location in receivePort.ReceiveLocations)
                    {
                        // Console.WriteLine(location.Name);

                        System.Diagnostics.Debug.WriteLine(location.Address);

                        if (location.Address != null)
                        {
                            BtsNode tn = new BtsNode(location.Address);
                            tn.ImageIndex = 3;
                            tn.SelectedImageIndex = 3;
                            tp.Nodes.Add(tn);

                            if (location.Enable == false)
                            {
                                tn.ForeColor = Color.LightSalmon;
                                tp.ForeColor = Color.LightSalmon;
                            }

                            GetLocationsForAddress(location.Address, tn);
                        }

                    }
                }

            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }


        }

        public void GetAllSendPortAdresses(BtsNode tp)
        {


            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING SEND LOCATIONS");
                //Enumerate the send Ports.
                foreach (SendPort sendPort in catalog.SendPorts)
                {

                    try
                    {
                        System.Diagnostics.Debug.WriteLine(sendPort.PrimaryTransport.Address);
                        System.Diagnostics.Debug.WriteLine(sendPort.Filter);


                        if (sendPort.PrimaryTransport.Address != null)
                        {
                            BtsNode tn = new BtsNode(sendPort.PrimaryTransport.Address);
                            tn.ImageIndex = 3;
                            tn.SelectedImageIndex = 3;
                            tp.Nodes.Add(tn);
                            GetLocationsForAddress(sendPort.PrimaryTransport.Address, tn);

                        }
                    }
                    catch
                    {
                        System.Diagnostics.Debug.WriteLine("Problem with Send Port" + sendPort.Name);
                    }
                }




            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }


        }

        public void GetLocationsForAddress(string targetAddress, BtsNode tp)
        {

            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING RECEIVE LOCATIONS");

                //Enumerate the receive locations in each of the receive ports.
                foreach (ReceivePort receivePort in catalog.ReceivePorts)
                {
                    //Console.WriteLine(receivePort.Name);
                    //Enumerate the receive locations.
                    foreach (ReceiveLocation location in receivePort.ReceiveLocations)
                    {
                        // Console.WriteLine(location.Name);

                        System.Diagnostics.Debug.WriteLine(location.Address);
                        if (location.Address != null && CompareBaseAddresses(location.Address ,targetAddress) )
                        {
                            BtsNode tn = new BtsNode(location.Name);
                            tn.ImageIndex = 5;
                            tn.SelectedImageIndex = 5;
                            tp.Nodes.Add(tn);

                            if (location.Enable == false) tn.ForeColor = Color.LightSalmon;

                            GetReceivePortForLocation(location.Name, tn);

                        }


                    }
                }


            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }

        }

        public void GetReceivePortForLocation(string strLocationName, BtsNode tp)
        {

            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING RECEIVE LOCATIONS");

                //Enumerate the receive locations in each of the receive ports.
                foreach (ReceivePort receivePort in catalog.ReceivePorts)
                {
                    //Console.WriteLine(receivePort.Name);
                    //Enumerate the receive locations.
                    foreach (ReceiveLocation location in receivePort.ReceiveLocations)
                    {
                        // Console.WriteLine(location.Name);

                        if (location.Name != null && location.Name == strLocationName)
                        {
                            BtsNode tn = new BtsNode(receivePort.Name);
                            tn.ImageIndex = 1;
                            tn.SelectedImageIndex = 1;
                            tp.Nodes.Add(tn);
                            if (location.Enable == false) tn.ForeColor = Color.LightSalmon;

                            GetFiltersForReceivePort(receivePort.Name, tn);
                            GetOrchestrationsForReceivePort(receivePort.Name, tn);

                        }


                    }
                }


            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }

        }

        public void GetOrchestrationsForReceivePort(string strReceivePortName, BtsNode tp)
        {

           
            try
            {




                System.Diagnostics.Debug.WriteLine("PROCESSING ORCHESTRATIONS");

                foreach (Microsoft.BizTalk.ExplorerOM.Application app in catalog.Applications)
                {

                    foreach (BtsOrchestration orch in app.Orchestrations)
                    {

                        System.Diagnostics.Debug.WriteLine(orch.FullName);

                        foreach (OrchestrationPort port in orch.Ports)
                        {


                            if (port.ReceivePort != null)
                            {

                                if (port.ReceivePort.Name == strReceivePortName)
                                {
                                    BtsNode tn = new BtsNode(orch.FullName);
                                    tn.ImageIndex = 0;
                                    tn.SelectedImageIndex = 0;
                                    tp.Nodes.Add(tn);
                                    if (orch.Status != OrchestrationStatus.Started) tn.ForeColor = Color.LightSalmon;

                                    GetSendPortsForOrchestration(orch.FullName, tn);
                                }

                            }


                        }
                    }
                }
                //Orchestrations


            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }

        }

        public void GetFiltersForReceivePort(string strPortName, BtsNode tp)
        {

            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING SEND LOCATIONS");
                //Enumerate the send Ports.
                foreach (SendPort sendPort in catalog.SendPorts)
                {

                    try
                    {
                        System.Diagnostics.Debug.WriteLine(sendPort.PrimaryTransport.Address);
                        System.Diagnostics.Debug.WriteLine(sendPort.Filter);

                        if (sendPort.Filter != null && sendPort.Filter != String.Empty)
                        {
                            if (sendPort.Filter.Contains("Value=\"" + strPortName))
                            {
                                BtsNode tn = new BtsNode(strPortName);
                                tn.ImageIndex = 4;
                                tn.SelectedImageIndex = 4;
                                tp.Nodes.Add(tn);

                                if (sendPort.Status!= PortStatus.Started) tn.ForeColor = Color.LightSalmon;



                                BtsNode tnp = new BtsNode(sendPort.Name);
                                tnp.ImageIndex = 2;
                                tnp.SelectedImageIndex = 2;

                                if (sendPort.Status!= PortStatus.Started) tnp.ForeColor = Color.LightSalmon;
                                tn.Nodes.Add(tnp);


                                GetAddressForSendPort(sendPort.Name, tnp);

                                //GetSendPortForFilter(strPortName, tn);
                            }
                        }
                    }
                    catch
                    {
                        System.Diagnostics.Debug.WriteLine("Problem with Send Port" + sendPort.Name);
                    }
                }

            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }



        }

        public void GetOrchestrationForFilter(string strFilterValue)
        {
        }

        public void GetSendPortForFilter(string strFilterValue, BtsNode tp)
        {
      
            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING SEND LOCATIONS");
                //Enumerate the send Ports.
                foreach (SendPort sendPort in catalog.SendPorts)
                {

                    try
                    {
                        System.Diagnostics.Debug.WriteLine(sendPort.PrimaryTransport.Address);
                        System.Diagnostics.Debug.WriteLine(sendPort.Filter);

                        if (sendPort.Filter != null && sendPort.Filter != String.Empty)
                        {
                            if (sendPort.Filter.Contains("Value=\"" + strFilterValue))
                            {
                                BtsNode tn = new BtsNode(sendPort.Name);
                                tn.ImageIndex = 2;
                                tn.SelectedImageIndex = 2;
                                tp.Nodes.Add(tn);
                                GetAddressForSendPort(sendPort.Name, tn);
                            }
                        }
                    }
                    catch
                    {
                        System.Diagnostics.Debug.WriteLine("Problem with Send Port" + sendPort.Name);
                    }
                }

            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }


        }

        public void GetSendPortsForOrchestration(string OrchestrationFullName, BtsNode tp)
        {


            try
            {

                System.Diagnostics.Debug.WriteLine("PROCESSING ORCHESTRATIONS");

                foreach (Microsoft.BizTalk.ExplorerOM.Application app in catalog.Applications)
                {

                    foreach (BtsOrchestration orch in app.Orchestrations)
                    {

                        System.Diagnostics.Debug.WriteLine(orch.FullName);

                        if (orch.FullName == OrchestrationFullName)
                        {
                            foreach (OrchestrationPort port in orch.Ports)
                            {


                                if (port.SendPort != null)
                                {
                                    try
                                    {

                                        BtsNode tn = new BtsNode(port.SendPort.Name);
                                        tn.ImageIndex = 2;
                                        tn.SelectedImageIndex = 2;
                                        tp.Nodes.Add(tn);

                                        if (port.SendPort.Status != PortStatus.Started) tn.ForeColor = Color.LightSalmon;

                                        GetAddressForSendPort(port.SendPort.Name, tn);

                                    }
                                    catch
                                    {

                                        System.Diagnostics.Debug.WriteLine("Problem with Send Port" + port.Name);
                                    }
                                }

                            }
                        }
                    }
                }
                //Orchestrations


            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }

        }

        public void GetAddressForSendPort(string strSendPortName, BtsNode tp)
        {

            try
            {


                System.Diagnostics.Debug.WriteLine("PROCESSING SEND LOCATIONS");
                //Enumerate the send Ports.
                foreach (SendPort sendPort in catalog.SendPorts)
                {

                    try
                    {

                        System.Diagnostics.Debug.WriteLine(sendPort.PrimaryTransport.Address);
                        System.Diagnostics.Debug.WriteLine(sendPort.Filter);

                        if (sendPort.Name == strSendPortName)
                        {
                            if (sendPort.PrimaryTransport.Address != null)
                            {
                                BtsNode tn = new BtsNode(sendPort.PrimaryTransport.Address);
                                tn.ImageIndex = 3;
                                tn.SelectedImageIndex = 3;
                                tp.Nodes.Add(tn);

                                if (sendPort.Status != PortStatus.Started) tn.ForeColor = Color.LightSalmon;

                                GetLocationsForAddress(sendPort.PrimaryTransport.Address, tn);

                                if (tn.Nodes.Count > 0)
                                {
                                    tn.BackColor = System.Drawing.Color.Lavender;
                                }
                            }
                        }
                    }
                    catch
                    {
                        System.Diagnostics.Debug.WriteLine("Problem with Send Port" + sendPort.Name);
                    }
                }



            }
            catch (Exception e)//If it fails, roll-back all changes.
            {
                throw e;
            }

        }



        public void GetAllNodes(List<BtsNode> NodeList, BtsNode tp)
        {
            if (NodeList == null)
            {
                NodeList = new List<BtsNode>();
            }

        

            foreach (BtsNode tn in tp.Nodes)
            {
                NodeList.Add(tn);
                GetAllNodes(NodeList, tn);
            }
        }

        public bool CompareBaseAddresses(string address1, string address2)
        {

            try
            {
                if (address1.ToLower() == address2.ToLower()) return true;

                //for them to be same first three letters need to be same
                if (address1.Substring(0, 3).ToLower() != address2.Substring(0, 3).ToLower()) return false;

                if (System.IO.Path.GetDirectoryName(address1).ToLower() == System.IO.Path.GetDirectoryName(address2).ToLower()) return true;

                int lastIndex = address1.LastIndexOf(@"/");

                if (lastIndex > 0)
                {
                    String address1Trimmed = address1.Remove(lastIndex);

                    if (address2.ToLower().Contains(address1Trimmed.ToLower())) return true;

                    return false;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(address1, address2);
                throw ex;
            }
            return false;
        }

    }


    public class BtsNode : TreeNode
    {
        List<BtsNode> NayborNodes;
        public BtsNode(string Text):base(Text)
        {
            
        }
    }



    public class AddressNode:BtsNode
    {
        public AddressNode(string Text): base(Text)
        { 
        }
        public ReceiveLocation receiveLocation;
    }
    public class ReceiveLocationNode:BtsNode
    {
        public ReceiveLocationNode(string Text): base(Text)
        { 
        }
         public ReceiveLocation location;
   }
    public class ReceivePortNode:BtsNode
    {
        public ReceivePortNode(string Text): base(Text)
        { 
        }
        public ReceivePort receivePort;
    }
    public class SendPortNode:BtsNode
    {  public SendPortNode(string Text): base(Text)
        { 
        }
        public SendPort  sendPort;
    }
    public class OrchestrationNode:BtsNode
    {
        public OrchestrationNode(string Text): base(Text)
        { 

        }
        public BtsOrchestration orchestration;
    }
    public class FilterNode:BtsNode
    {
        public FilterNode(string Text)
            : base(Text)
        { 
        }
        public SendPort sendPort;
    }
}
